import menuTreeData from '@/json/tree'
import shellDescData from '@/json/birds'

function getShellModel() {
    const array = [];
    const jsonList = [];


    menuTreeData.forEach((item) => {
        if (item.length > 1) {
            array.push({
                num: item[0].length,
                fieldName: item[1],
                id: item[3],
            });
        }
    });



    let detail = {
        ganId: '',
        ganName: '',
        muId: '',
        muName: '',
        keId: '',
        keName: '',
        shuName: '',
        shuId: '',
    };
    array.forEach((item) => {
        if (item.num === 0) {
            Object.assign(detail, {
                ganId: item.id,
                ganName: item.fieldName,
                muName: '',
                muId: '',
                keId: '',
                keName: '',
                shuName: '',
                shuId: '',
            });
        }

        if (item.num === 4) {
            // set
            Object.assign(detail, {
                muName: item.fieldName,
                muId: item.id,
            });
        }

        if (item.num === 8) {
            // set
            Object.assign(detail, {
                keName: item.fieldName,
                keId: item.id,
            });
        }

        if (item.num === 12) {
            Object.assign(detail, {
                shuName: item.fieldName,
                shuId: item.id,
            });

            jsonList.push({
                ...detail,
                items: shellDescData.filter(shellItem => {
                    let shuId = shellItem[2].split(' ')[0]
                    if (detail.shuId === shuId) {
                        return true
                    } else {
                        return false
                    }
                })
            });
        }
        // console.log(detail);
    });

    // console.log(array.slice(0, 10), shellDescData.slice(0, 10), jsonList.slice(0, 10));


    const tree = {};

    jsonList.forEach((shell) => {
        const {
            ganId,
            ganName,
            muId,
            muName,
            keId,
            keName,
            shuId,
            shuName,
            items = []
        } = shell;

        if (!tree[ganId]) {
            tree[ganId] = {
                _name: ganName,
            };
        }
        const gan = tree[ganId];

        if (!gan[muId]) {
            gan[muId] = {
                _name: muName,
            };
        }
        const mu = gan[muId];

        if (!mu[keId]) {
            mu[keId] = {
                keId,
                keName,
                shells: [],
            };
        }
        const ke = mu[keId];

        ke.shells.push({
            ganId,
            ganName,
            muId,
            muName,
            keId,
            keName,
            shuId,
            shuName,
            items
        });
    });

    return {
        array,
        menuTree: tree,
        jsonList,
    };
}

export default getShellModel;
